package controllers;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import services.user.UserService;
import beans.UserVO;

@Controller
public class EjecutarAccionUserController {

    @Autowired
    UserService userService;

    @RequestMapping(value = "public/userAction", method = RequestMethod.GET)
    public ModelAndView elegirAccion(@RequestParam String submitUserAction) {
        ModelAndView mv = new ModelAndView();

        if (submitUserAction.equals("alta")) {
            mv.setViewName("altaUser");
        }
        if (submitUserAction.equals("baja")) {
            mv.setViewName("bajaUser");
        }
        if (submitUserAction.equals("modificacion")) {
            mv.setViewName("modificacionUser");
        }
        if (submitUserAction.equals("seleccion")) {
            mv.setViewName("seleccionUser");
        }
        return mv;
    }

    @RequestMapping(value = "public/ejecutarUserAction", method = RequestMethod.GET)
    public ModelAndView ejecutarAccion(@ModelAttribute UserVO userVO,
            @RequestParam String submitUserABM) throws Exception {
        ModelAndView mv = new ModelAndView();

        if (submitUserABM.equals("alta")) {
            System.out.println("ENTRE A ALTA");

            this.getUserService().insert(userVO);

            mv.addObject("paramReasonOK", "ALTA");
            mv.setViewName("salidaOK");
        }

        if (submitUserABM.equals("baja")) {
            System.out.println("ENTRE A BAJA");

            this.getUserService().delete(userVO);

            mv.addObject("paramReasonOK", "BAJA");
            mv.setViewName("salidaOK");
        }

        if (submitUserABM.equals("modificacion")) {
            System.out.println("ENTRE A MODIFICACION");

            this.getUserService().update(userVO);

            mv.addObject("paramReasonOK", "MODIFICACION");
            mv.setViewName("salidaOK");
        }

        if (submitUserABM.equals("seleccion")) {
            System.out.println("ENTRE A SELECCION");
            List<UserVO> listUsers = new ArrayList<UserVO>();

            if (!userVO.getName().equals("")) {
                listUsers = (List<UserVO>) this.getUserService().selectByName(userVO);

                mv.addObject("listUsers", listUsers);

                mv.setViewName("seleccionadoUser");
            } else {
                listUsers = (List<UserVO>) this.getUserService().selectAll();

                mv.addObject("listUsers", listUsers);

                mv.setViewName("seleccionadoUser");                
            }
        }
        return mv;
    }

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}